# Get current shell name.
shell_name=$(ps -o comm= $$)

case "${shell_name}" in
    "-"*)
        ;;
    *)
        shell_name=$(basename $shell_name)
        ;;
esac

# Get the absolute path of this script according to different shells.
case "${shell_name}" in
    "zsh" | "-zsh")
        source_dir="${(%):-%N}"
        ;;
    "sh" | "-sh" | "bash" | "-bash")
        source_dir="${BASH_SOURCE[0]}"
        ;;
    *)
        echo "[ERROR] Unsupported shell: ${shell_name}, please switch to bash, sh or zsh."
        return 1
        ;;
esac

if [ -L ${source_dir} ]; then
    if command -v realpath 2>&1 >/dev/null; then
        source_dir=$(realpath "${source_dir}")
    else
        echo '`realpath` is not found, setup may not process properly.'
    fi
fi
script_dir=$(cd "$(dirname "${source_dir}")"; pwd)

export PATH=${PATH}:${script_dir}/bin

OS="$(uname -s)"
if [ "$OS" = "Darwin" ]; then
    export DYLD_LIBRARY_PATH=${DYLD_LIBRARY_PATH}:${script_dir}/libs/magichelper
    rm -rf ${script_dir}/libs/magichelper/libpcre2-8.0.dylib
    ln -s ${script_dir}/libs/magichelper/libpcre2-8.0.13.0.dylib ${script_dir}/libs/magichelper/libpcre2-8.0.dylib
    xattr -dr com.apple.quarantine ${script_dir}/* &> /dev/null || true
elif [ "$OS" = "Linux" ]; then
    export LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:${script_dir}/libs/magichelper
fi
